package com.foreverwin.jzymes.common.service.impl;

import com.foreverwin.jzymes.common.mapper.ItemAdviceMapper;
import com.foreverwin.jzymes.common.service.IItemAdviceService;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;

@Service
public class ItemAdviceServiceImpl implements IItemAdviceService {

    @Autowired
    ItemAdviceMapper itemAdviceMapper;

    /**
     * 检查物料是否存在
     *
     * @param site
     * @param item
     * @return
     */
    public Map<String,Object> existItem( String site, String item ){
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ) );
        Assert.hasText( item, I18nUtil.getI18nText( "common.itemNotBlank" ) );
        Map<String,Object> itemMap = itemAdviceMapper.selectItemCurrRevMap( site, item );
        Assert.notNull( itemMap, I18nUtil.getI18nText( "common.itemNotExist", new String[]{ item } ) );

        return itemMap;
    }

    /**
     * 检查物料是否存在
     *
     * @param itemBo
     * @return
     */
    public Map<String,Object> existItem( String itemBo ){
        Assert.hasText( itemBo, I18nUtil.getI18nText( "common.itemNotBlank" ) );
        String[] itemParts = BOHandleUtil.splitItemHandle( itemBo );
        if( itemParts.length == 3 && ( itemParts[2].equals( "*" ) || itemParts[2].equals( "#" ) ) ){
            return existItem( itemParts[0], itemParts[1] );
        }
        Map<String,Object> itemMap = itemAdviceMapper.selectItemMap( itemBo );
        Assert.notNull( itemMap, I18nUtil.getI18nText( "common.itemNotExist", new String[]{ itemBo } ) );
        return itemMap;
    }

    /**
     * 新增物料
     *
     * @param itemMap
     * @return
     */
    public int insertItem( Map<String,Object> itemMap ){
        return itemAdviceMapper.insertItem( itemMap );
    }

    /**
     * 修改物料
     *
     * @param itemMap
     * @return
     */
    public int updateItem( Map<String,Object> itemMap ){
        return itemAdviceMapper.updateItem( itemMap );
    }

    /**
     * 原材料
     *
     * @param site
     * @return
     */
    public List< Map<String,Object> > findRawMaterialList( String site ){ return itemAdviceMapper.findRawMaterialList( site ); }

    /**
     * 查找物料清单的物料
     *
     * @param site
     * @param bom
     * @return
     */
    public List< Map<String,Object> > findBomcomponentlList( String site , String bom){ return itemAdviceMapper.findBomcomponentlList(site , bom); }
}
